<?php
declare (strict_types = 1);
namespace app\api\controller;
use app\api\BaseController;
// use app\api\middleware\Auth;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\facade\Db;
// use think\Request;        // 依赖注入需要
use think\facade\Request; // 门面方式需要

class MyInfo extends BaseController
{
	protected $middleware = [
    	
    ];
	// 获取收货人地址列表
	public function index(){ 
		$userid = request()->param('id');
		// 默认优先显示，一样的按照创建时间排序
		$list = Db::name('useraddress')->where('user_id',$userid)->order('isDefault desc,create_time desc')->select()->toArray();
		return json(['code'=>200,'msg'=>'获取成功','data'=>$list]);  
	}
	// 新增收货人
	  public function add(){
		  if(request()->isPost()) {
			  // 获取全部数据
			  $data = request()->param();
			  $data['create_time'] = time();
			  // 设置默认地址
			  if($data['isDefault']==true){
				  $update=Db::name('useraddress')->where('user_id',$data['user_id'])->update(['isDefault'=>0]);
			  }
			  $res = Db::name('useraddress')->insert($data);
			  if($res){
				  return json(['code'=>200,'msg'=>'添加成功']);
			  }else{
				  return json(['code'=>400,'msg'=>'添加失败']);
			 }
		}else{
			return json(['code'=>400,'msg'=>'请求方式错误']);
		}
	}
	// 编辑收货人
	public function edit(){
		if(request()->isPost()){
			$id = request()->param('id');
			$data = request()->param();
			$data['update_time'] = time();
			if($data['isDefault']==true){
			$update=Db::name('useraddress')->where('user_id',$data['user_id'])->update(['isDefault'=>0]);
			}
			$res = Db::name('useraddress')->where('id',$id)->where('user_id',$data['user_id'])->update($data);
			if($res){
			     return json(['code'=>200,'msg'=>'修改成功']);
			}else{
				return json(['code'=>400,'msg'=>'修改失败']);
		   }
		}else{
			return json(['code'=>400,'msg'=>'请求方式错误']);
		}
	}
	// 删除收货人
	public function del(){
		$id = request()->param('id');
		$res = Db::name('useraddress')->where('id',$id)->delete();
		if($res){
			return json(['code'=>200,'msg'=>'删除成功']);
		}else{
			return json(['code'=>400,'msg'=>'删除失败']);
		}
	}
	// 获取收货人地址详情
	public function info(){
		$id =  request()->param('id');
	    $res = Db::name('useraddress')->where(['id'=>$id])->find();
		return json(['code'=>200,'msg'=>'请求成功','data'=>$res]);
	}
	// 设置默认地址
	public function setDefault(){
		$id =  request()->param('id');
		$user_id = request()->param('user_id');
		// 查询当前是否已设置当前为默认地址
		$check = Db::name('useraddress')->where('id',$id)->where('user_id',$user_id)->where('isDefault',1)->find();
		if($check){
			return json(['code'=>400,'msg'=>'已是默认']);
		}else{
		$res = Db::name('useraddress')->where('id',$id)->where('user_id',$user_id)->update(['isDefault'=>1]);
		$update=Db::name('useraddress')->where('user_id',$user_id)->where('id','<>',$id)->update(['isDefault'=>0]);
		if($res){ 
		return json(['code'=>200,'msg'=>'设置成功']);
		}
	   }
    }
	// 获取默认地址
	public function defaultaddress(){
		$userid =  request()->param('userid');
		$info=Db::name('useraddress')->where(['user_id'=>$userid,'isDefault'=>1])->find();
		if($info){
			return json(['code'=>200,'msg'=>'请求成功','data'=>$info]);
		}else{
			return json(['code'=>400,'msg'=>'暂无默认地址']);
		}
	}
}